<?php

/**
 * Description of guestUpdateAction
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class guestUpdateAction extends sfAction {
    
    public function execute($request) {
        $this->form = new ReservationGuestForm();

        if ($this->processGuestReservation($request, $this->form)) {
            $token = $request->getParameter('token');
            
            SqlEngineCore::get()->update(
                    array('table' => 'reservation',
                          'columns' => array(
                                'menu_id' => $request->getParameter('menu_id', null),
                                'date' => $this->form->getValue('date'),
                                'occasion' => $this->form->getValue('occasion'),
                                'time' => $this->form->getValue('time_hour') . ':' . $this->form->getValue('time_minute'),
                                'person_no' => array('value' => $this->form->getValue('person_no'), 'type' => PDO::PARAM_INT),
                                'salutation' => array('value' => $this->form->getValue('salutation'), 'type' => PDO::PARAM_INT),
                                'name_last' => $this->form->getValue('name_last'),
                                'name_first' => $this->form->getValue('name_first'),
                                'name_company' => $this->form->getValue('name_company'),
                                'address' => $this->form->getValue('address'),
                                'phone' => $this->form->getValue('phone'),
                                'mobile' => $this->form->getValue('mobile'),
                                'email' => $this->form->getValue('email'),
                                'comments_diet' => $this->form->getValue('comments_diet'),
                                'comments_general' => $this->form->getValue('comments_general')),
                          'where' => array(
                                'token' => $token)));

            /* remeber user options */
            $cookie_data = array('gender' => $this->form->getValue('salutation'),
                                 'name_last' => $this->form->getValue('name_last'),
                                 'name_first' => $this->form->getValue('name_first'),
                                 'name_company' => $this->form->getValue('name_company'),
                                 'address' => $this->form->getValue('address'),
                                 'phone' => $this->form->getValue('phone'),
                                 'mobile' => $this->form->getValue('mobile'),
                                 'email' => $this->form->getValue('email'),
                                 'comments_diet' => $this->form->getValue('comments_diet'));
            
            $reservation = SqlEngine::getReservations(array('token' => $token));
            Util::send_update_email($reservation[0]);

            setcookie('guest_user_options', json_encode($cookie_data), time() + 60 * 60 * 24 * 365, '/');

            $this->redirect('@thankyou_update_page');
        }
        else {
            $this->setTemplate('deepLinkReservationShow');
        }
    }

    protected function processGuestReservation(sfWebRequest $request, sfForm $form) {
        $params = $request->getParameter($form->getName());
        $params['date'] = Util::convertDateClientServer($params['date'], 'nl');
        $form->bind($params);
        
        $is_block_time = Util::is_block_time($params['date'], 
                                             $params['time_hour'],
                                             $params['time_minute'],
                                             $this->getUser());

        $is_seat_limit = Util::is_seat_limit($params['person_no'],
                                             $params['date'],
                                             $params['time_hour'],
                                             $params['time_minute'],
                                             $this->getUser());

        if ($is_block_time['is_block_time']) {
            $request->setParameter('block_time', true);
            $request->setParameter('block_time_reason', $is_block_time['reason']);
        }
        else if ($is_seat_limit)
            $request->setParameter('seat_limit', true);

        return $form->isValid() && !$is_block_time['is_block_time'] && !$is_seat_limit;
    }
}
